package com.www.applyword.ui.my;

import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.bean.ResumeBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/24 0024 17:01
 * version: 1.0
 */
public class EvaluationActivity extends BaseActivity {
    @BindView(R.id.tv_entrance)
    TextView tvEntrance;
    @BindView(R.id.tv_entrance_start_time)
    TextView tvEntranceStartTime;
    @BindView(R.id.tv_entrance_end_time)
    TextView tvEntranceEndTime;
    @BindView(R.id.et_school)
    EditText etSchool;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_online_experience)
    EditText etOnlineExperience;
    @BindView(R.id.sb_delete)
    SuperButton sbDelete;
    private String typeName;

    OptionsPickerView pvOptions;
    //自行添加一些string
    private List<String> gradeData = new ArrayList<>();
    private ResumeBean.ResumeSchoolBean evaluationBean;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.evaluation_activity);
        setTitle("教育经历");
        setRringTitle("保存");
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveResumeSchool();
            }
        });
    }


    @Override
    protected void loadData() {
        evaluationBean = (ResumeBean.ResumeSchoolBean) getIntent().getSerializableExtra("evaluationBean");
        if (null != evaluationBean) {
            etSchool.setText(evaluationBean.getResumeSchool_Name());
            tvEntranceStartTime.setText(evaluationBean.getResumeSchool_StartTime().split(" ")[0]);
            tvEntranceEndTime.setText(evaluationBean.getResumeSchool_EndTime().split(" ")[0]);
            etName.setText(evaluationBean.getResumeSchool_Major());  //主修
            tvEntrance.setText(evaluationBean.getResumeSchool_Education());
            etOnlineExperience.setText(evaluationBean.getResumeSchool_Experience());
            sbDelete.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.tv_entrance, R.id.tv_entrance_start_time, R.id.tv_entrance_end_time,R.id.sb_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_entrance:
                typeName = "学历";
                showJobType(2);
                break;
            case R.id.tv_entrance_start_time:
                typeName = "开始时间";
                showTime(1);
                break;
            case R.id.tv_entrance_end_time:
                typeName = "结束时间";
                showTime(2);
                break;
            case R.id.sb_delete:
                delResumeSchool();
                break;
            default:
        }
    }

    private void delResumeSchool() {
        Map<String, Object> params = new HashMap<>();
        params.put("ResumeSchoolID",evaluationBean.getResumeSchool_ID());
        HttpClient.get(EvaluationActivity.this, Constants.DELRESUMESCHOOL, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                Utils.showToastSafe("删除成功");
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.DEL_RESUMESCHOOL);
                EventBus.getDefault().post(messageEvent);
                finish();
            }
        });
    }

    private void showJobType(int type) {
        hideInput2();
        if (type == 1) {
            gradeData.clear();
            gradeData.add("兼职");
            gradeData.add("全职");
        } else if (type == 2) {
            gradeData.clear();
            gradeData.add("高中以下");
            gradeData.add("高中");
            gradeData.add("中专/技校");
            gradeData.add("大专");
            gradeData.add("本科");
            gradeData.add("硕士");
            gradeData.add("博士");
        } else if (type == 3) {
            gradeData.clear();
            gradeData.add("在读");
            gradeData.add("已毕业");
        } else if (type == 4) {
            gradeData.clear();
            gradeData.add("不限");
            gradeData.add("18-30岁");
            gradeData.add("30-40岁");
            gradeData.add("50岁以下");
        }
        pvOptions = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            if (type == 2) {
                tvEntrance.setText(gradeData.get(options1));
            }
        })
                .setTitleText(typeName)
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(10)//确定和取消文字大小
                .setSubmitColor(0xffFF6767)//确定按钮文字颜色
                .setCancelColor(0xff999999)//取消按钮文字颜色
                .setTitleBgColor(0xffF4F4F4)//标题背景颜色 Night mode
                .setTitleSize(10)
                .setContentTextSize(12)//滚轮文字大小
                .setTextColorCenter(0xff333333)
                .setTextColorOut(0x999999)
                .setDividerColor(0xffEEEEEE)
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(0, 0, 0)  //设置默认选中项
                .setOutSideCancelable(true)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .setLineSpacingMultiplier((float) 2.5) //设置item的高度
                .build();
        pvOptions.setPicker(gradeData);
        pvOptions.show();
    }

    private void hideInput2() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    private void showTime(int type) {
        hideInput2();
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(1949, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2030, 1, 1);

        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) ->
        {
            if (type == 1) {
                tvEntranceStartTime.setText(Utils.getTime2(date));
            } else {
                tvEntranceEndTime.setText(Utils.getTime2(date));
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})//默
                .setTitleText(typeName)
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(10)//确定和取消文字大小
                .setSubmitColor(0xffFF6767)//确定按钮文字颜色
                .setCancelColor(0xff999999)//取消按钮文字颜色
                .setTitleBgColor(0xffF4F4F4)//标题背景颜色 Night mode
                .setTitleSize(10)
                .setContentTextSize(12)//滚轮文字大小
                .setTextColorCenter(0xff333333)
                .isCyclic(true)//是否循环滚动
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setTextColorOut(0x999999)
                .setDividerColor(0xffEEEEEE)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setOutSideCancelable(true)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier((float) 2.5) //设置item的高度
                .build();
        //注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.setDate(Calendar.getInstance());
        pvTime.show();
    }

    private void saveResumeSchool() {
        if (TextUtils.isEmpty(etName.getText().toString().trim())) {
            Utils.showToastSafe("请填写学校名称");
            return;
        }
       /* if (TextUtils.isEmpty(tvEntranceStartTime.getText().toString().trim())) {
            Utils.showToastSafe("请选择开始时间");
            return;
        }*/
        if (TextUtils.isEmpty(tvEntrance.getText().toString().trim())) {
            Utils.showToastSafe("请选择学历");
            return;
        }
        if (TextUtils.isEmpty(tvEntranceEndTime.getText().toString().trim())) {
            Utils.showToastSafe("请选择结束时间");
            return;
        }
      /*  if (TextUtils.isEmpty(tvJobType.getText().toString().trim())) {
            Utils.showToastSafe("请选择工作类型");
            return;
        }*/
        if (TextUtils.isEmpty(tvEntranceEndTime.getText().toString().trim())) {
            Utils.showToastSafe("请选择结束时间");
            return;
        }


        Map<String, Object> params = new HashMap<>();
        params.put("ResumeSchool_ID", "");
        params.put("ResumeSchool_UserID", SPUtil.getString(Constants.USERID));
        params.put("ResumeSchool_Name", etSchool.getText().toString().trim());
        params.put("ResumeSchool_StartTime", tvEntranceStartTime.getText().toString());
        params.put("ResumeSchool_EndTime", tvEntranceEndTime.getText().toString());
        params.put("ResumeSchool_Major", etName.getText().toString());
        params.put("ResumeSchool_Education", tvEntrance.getText().toString());
        params.put("ResumeSchool_Experience", etOnlineExperience.getText().toString());

        HttpClient.post(EvaluationActivity.this, Constants.RESUMESCHOOL, params, new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                Utils.showToastSafe("保存成功");
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.DEL_RESUMESCHOOL);
                EventBus.getDefault().post(messageEvent);
                finish();
            }
        });
    }


}
